"""
Case Type   : 全链路跟踪
Case Name   : 新增guc参数track_stmt_trace_size范围验证
Create At   : 2025-10-09
Owner       : sungang14
Description :
    1、会话中查询track_stmt_trace_size默认值show track_stmt_trace_size;
    2、设置为0：gs_guc reload -N all -I all -c "track_stmt_trace_size = 0"
    3、设置为负数：gs_guc reload -N all -I all -c "track_stmt_trace_size = -1"
    4、设置范围上限：gs_guc reload -N all -I all -c "track_stmt_trace_size = 100000000"
    5、设置超过范围上限：gs_guc reload -N all -I all -c "track_stmt_trace_size = 100000001"
    6、设置为浮点数：gs_guc reload -N all -I all -c "track_stmt_trace_size = 100.4"
    7、设置为字符串：gs_guc reload -N all -I all -c "track_stmt_trace_size = xxxx"
Expect      :
    1、4096
    2、设置成功
    3、设置失败，不在范围
    4、设置成功
    5、设置失败，不在范围
    6、设置失败
    7、设置失败
History     :
"""

import os
import unittest
from datetime import datetime

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node

logger = Logger()


class SlowSQLTestCase(unittest.TestCase):
    def setUp(self):
        logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.comsh = CommonSH('PrimaryDbUser')
        self.node = Node("PrimaryDbUser")
        status = self.comsh.get_db_cluster_status('status')
        self.assertTrue(status)
        self.show_trace_size = 'show track_stmt_trace_size;'
        self.set_trace_size_01 = "track_stmt_trace_size = 0"
        self.set_trace_size_02 = 'track_stmt_trace_size = -1'
        self.set_trace_size_03 = 'track_stmt_trace_size = 100000000'
        self.set_trace_size_04 = 'track_stmt_trace_size = 100000001'
        self.set_trace_size_05 = 'track_stmt_trace_size = 100.4'
        self.set_trace_size_06 = 'track_stmt_trace_size = xxxx'
        self.set_trace_size_07 = 'track_stmt_trace_size = 4096'
        self.error_msg_01 = f'is outside the valid range for parameter "track_stmt_trace_size" (0 .. 100000000).'
        self.error_msg_02 = f'requires an integer value.'

    def test_slowsql(self):
        text = '--step1:会话中查询track_stmt_trace_size默认值show track_stmt_trace_size;expect:4096--'
        logger.info(text)
        show_cmd = self.comsh.execut_db_sql(self.show_trace_size)
        logger.info(show_cmd)
        self.assertEquals('4096', show_cmd.splitlines()[-2].strip(), '执行失败' + text)

        text = "--step2:设置track_stmt_trace_size为0';expect:设置成功--"
        logger.info(text)
        set_cmd1 = self.comsh.execute_gsguc('reload',
                                            self.constant.GSGUC_SUCCESS_MSG,
                                            self.set_trace_size_01)
        logger.info(set_cmd1)
        self.assertTrue(set_cmd1)

        text = "--step3:设置track_stmt_trace_size为负数';expect:设置失败，不在范围--"
        logger.info(text)
        set_cmd2 = self.comsh.execute_gsguc('reload',
                                            self.constant.GSGUC_SUCCESS_MSG,
                                            self.set_trace_size_02)
        logger.info(set_cmd2)
        self.assertFalse(set_cmd2)

        text = "--step4:设置track_stmt_trace_size为范围上限';expect:设置成功--"
        logger.info(text)
        set_cmd3 = self.comsh.execute_gsguc('reload',
                                            self.constant.GSGUC_SUCCESS_MSG,
                                            self.set_trace_size_03)
        logger.info(set_cmd3)
        self.assertTrue(set_cmd3)

        text = "--step5:设置track_stmt_trace_size为超过范围上限';expect:设置失败，不在范围--"
        logger.info(text)
        set_cmd4 = self.comsh.execute_gsguc('reload',
                                            self.constant.GSGUC_SUCCESS_MSG,
                                            self.set_trace_size_04)
        logger.info(set_cmd4)
        self.assertFalse(set_cmd4)

        text = "--step6:设置track_stmt_trace_size为浮点数';expect:设置失败，不在范围--"
        logger.info(text)
        set_cmd5 = self.comsh.execute_gsguc('reload',
                                            self.constant.GSGUC_SUCCESS_MSG,
                                            self.set_trace_size_05)
        logger.info(set_cmd5)
        self.assertFalse(set_cmd5)

        text = "--step7:设置track_stmt_trace_size为字符串';expect:设置失败，不在范围--"
        logger.info(text)
        set_cmd6 = self.comsh.execute_gsguc('reload',
                                            self.constant.GSGUC_SUCCESS_MSG,
                                            self.set_trace_size_06)
        logger.info(set_cmd6)
        self.assertFalse(set_cmd6)

    def tearDown(self):
        text = '--step8:清理环境，恢复默认值;expect:成功--'
        logger.info(text)
        self.comsh.execute_gsguc('reload',
                                 self.constant.GSGUC_SUCCESS_MSG,
                                 self.set_trace_size_07)
        show_res = self.comsh.execut_db_sql(self.show_trace_size)
        logger.info(show_res)
        self.assertEquals('4096', show_res.splitlines()[-2].strip(),
                          '执行失败' + text)
        logger.info(f'-----{os.path.basename(__file__)} end-----')
